package it.cpyx.appengine.server.data.utils;

import it.cpyx.appengine.server.data.PMF;
import it.cpyx.appengine.server.data.TextData;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Text;
import com.google.appengine.api.users.User;

public class TextDataUtils {
	
	public static Long insert(String urlExtension,Text content, String pass, String title, User owner){
		TextData textData=new TextData(urlExtension, content, pass, title, owner);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Long id=pm.makePersistent(textData).getId();
		pm.close();
		return id;
	}
	
	public static void addLabelDataIdToTextData(Long textDataId,Long labelDataId){
		TextData textData=getById(textDataId);
		textData.addLabelDataList(labelDataId);
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(textData);
		pm.close();		
	}
	
	public static TextData getById(Long id){		
		PersistenceManager pm = PMF.get().getPersistenceManager();
    	TextData t=null;
    	try {
    		t = pm.getObjectById(TextData.class, id);
    	} catch (Exception ex) {
    		System.out.println(ex.toString());
    	} finally {
    		pm.close();
    	}
    	return t;
	}
	
	public static List<TextData> getAll(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + TextData.class.getName();
	    List<TextData> textDatas = (List<TextData>) pm.newQuery(query).execute();
	    List<TextData> textData=textDatas;
	    pm.close();	    
	    return textData;
	}
	
	public static TextData getByUrlExtension(String urlExtension){		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		TextData t=null;
    	Query query = pm.newQuery(TextData.class);
		query.declareParameters("java.lang.String urltxt");
		query.setFilter("urlExtension == urltxt");
		List<TextData> results = (List<TextData>)query.execute(urlExtension);
	    List<TextData> textList=results;
	    for(TextData l2:textList){
	    	t=l2;
	    }
	    pm.close();
    	return t;
	}
	
	public static String generateUniqueUrlExtension(String url){
    	String model="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOQPRSTUVWXYZ1234567890";
    	Random rnd=new Random();
    	if(url.equals("")){
    		url+=model.charAt(rnd.nextInt(model.length()));
    	}
    	StringBuffer urlHelper=new StringBuffer("");
		for(int j=0;j<url.length()-1;j++){
			urlHelper.append(url.charAt(j));
		}
		for(int i=0;i<5;i++){
			if(getByUrlExtension(url)!=null)
				url=urlHelper.toString()+model.charAt(rnd.nextInt(model.length()));
			else
				i=10;
		}
    	while(getByUrlExtension(url)!=null){
    		url+=model.charAt(rnd.nextInt(model.length()));
    	}
    	
    	return url;
    }
	
    public static int getSizeOfTextData(){
    	PersistenceManager pm = PMF.get().getPersistenceManager();
        String query = "select from " + TextData.class.getName();
        List<TextData> texts = (List<TextData>) pm.newQuery(query).execute();
        return texts.size();
    }
    
    public static void updateTextsHitAndLastEntry(Long id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.currentTransaction().begin();
			TextData t = pm.getObjectById(TextData.class, id);
			t.setHit(t.getHit()+1);
			t.setLastEntry(new Date());
			pm.makePersistent(t);
			pm.currentTransaction().commit();
		} catch (Exception ex) {
			pm.currentTransaction().rollback();
			System.out.println("TextDataUtils.updateTextsHit"+id+":"+ex.toString());
		} finally {
			pm.close();
		}
	}
}
